package com.luyuan.crm.member.service.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.hibernate.criterion.Order;

import com.luyuan.crm.member.model.Member;
import com.luyuan.crm.member.model.MemberIntegralRecord;
import com.luyuan.crm.member.service.MemberIntegralRecordService;
import com.ruijie.foundation.dao.support.HibernateEntityDao;
import com.ruijie.foundation.query.Page;
import com.ruijie.foundation.query.criteria.CriteriaQueryTemplate;
import com.ruijie.foundation.query.criteria.CriteriaTemplate;

public class MemberIntegralRecordServiceImpl extends HibernateEntityDao<MemberIntegralRecord> implements
        MemberIntegralRecordService {

    @Override
    public void addMemberIntegralRecord(MemberIntegralRecord usedRecord) {
        usedRecord.setUsedTime(new Date());
        save(usedRecord);
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<MemberIntegralRecord> getByMember(Member member) {
        return find("member", member);
    }

    @Override
    public Page pagedQuery(CriteriaTemplate c, int page, int pageSize, List<Order> orders) {
        if (orders.isEmpty()) {
            orders.add(Order.desc("usedTime"));
        }
        CriteriaQueryTemplate qt = new CriteriaQueryTemplate(getSessionFactory());
        return qt.pagedQuery(c, page, pageSize, orders);
    }

    @Override
    public void clearLog() {
        Calendar ca = Calendar.getInstance();// 得到一个Calendar的实例
        ca.setTime(new Date()); // 设置时间为当前时间
        ca.add(Calendar.YEAR, -1); // 年份减1
        Date lastMonth = ca.getTime(); // 结果
        logger.info("clean MemberIntegralRecord before: " + lastMonth);
        update("delete MemberIntegralRecord where used_time < ?", lastMonth);
    }

}
